package org.toolbook.controller;

import java.util.Date;

import org.toolbook.interceptor.LoginInterceptor;
import org.toolbook.model.NewWord;
import org.toolbook.model.User;
import org.toolbook.util.FileUtils;
import org.toolbook.util.LogUtil;
import org.toolbook.util.PropUtil;
import org.toolbook.validator.AddNewWordValidator;
import org.toolbook.validator.UpdateNewWordValidator;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
/**
 * 新词条控制器
 * @author meigang
 * @date 2015-07-02 00:06
 *
 */
@Before(LoginInterceptor.class)
public class NewWordController extends Controller{
	private static String tablePre = PropUtil.getProperty("tablePre");
	/**
	 * 词条首页
	 */
	public void index(){
		//查询newword分页
		Integer pageNumber = getParaToInt("page");
		if(pageNumber == null){
			pageNumber = 1;
		}
		
		Page<NewWord> newWordPage = NewWord.dao.paginate(pageNumber, 
				Integer.parseInt(PropUtil.getProperty("NEW_WORD_LIST_PAGE_SIZE")),
				"select *", "from "+tablePre+"new_word order by isTop desc,publishTime desc");
		setAttr("newWordPage", newWordPage);
		render("/WEB-INF/pages/fc/newword/list.jsp");
	}
	/**
	 * 跳转到添加词条界面
	 */
	public void toAdd(){
		LogUtil.log.info("跳转到add newword界面");
		render("/WEB-INF/pages/fc/newword/add.jsp");
	}
	/**
	 * 添加词条
	 */
	@Before(AddNewWordValidator.class)
	public void add(){
		User u = getSessionAttr(PropUtil.getProperty("SESSION_LOGIN_KEY"));
		NewWord word = new NewWord().set("title", getPara("title"))
				.set("lookPhoto", getPara("lookPhotoFilePath"))
				.set("note", getPara("note"))
				.set("content", getPara("content"))
				.set("publishTime", new Date())
				.set("updateTime", new Date())
				.set("readCount", 0)
				.set("isTop", 0)
				.set("isShare", 0)
				.set("checkState", 0)
				.set("belong", getPara("belong"))
				.set("userId", u.get("id"));
		word.save();
		LogUtil.log.info("用户[ "+ u.get("username")+" ]新增词条成功");
		index();
	}
	/**
	 * 显示词条页
	 */
	public void show(){
		int id = getParaToInt(0);
		NewWord word = NewWord.dao.findById(id);
		//更新阅读次数
		word.set("readCount", word.getInt("readCount")+1).update();
		setAttr("newWord", word);
		LogUtil.log.info("查看newword[id:"+id+"]");
		render("/WEB-INF/pages/fc/newword/show.jsp");
	}
	/**
	 * 删除
	 */
	public void delete(){
		int id = getParaToInt(0);
		NewWord word = NewWord.dao.findById(id);
		FileUtils.deleteFile(word.getStr("lookPhoto"));
		word.delete();
		LogUtil.log.info("删除newword[id:"+id+"]");
		index();
	}
	/**
	 * 修改
	 */
	public void toUpdate(){
		int id = getParaToInt(0);
		NewWord word = NewWord.dao.findById(id);
		setAttr("newWord", word);
		LogUtil.log.info("去修改界面newword[id:"+id+"]");
		render("/WEB-INF/pages/fc/newword/update.jsp");
	}
	/**
	 * 修改词条
	 */
	@Before(UpdateNewWordValidator.class)
	public void update(){
		User u = getSessionAttr(PropUtil.getProperty("SESSION_LOGIN_KEY"));
		NewWord word = NewWord.dao.findById(getParaToInt("id"));
		word.set("title", getPara("title"))
				.set("lookPhoto", getPara("lookPhotoFilePath"))
				.set("note", getPara("note"))
				.set("content", getPara("content"))
				.set("updateTime", new Date())
				.set("belong", getPara("belong"));
		word.update();
		LogUtil.log.info("用户[ "+ u.get("username")+" ]修改newword[id:"+word.getInt("id")+"]词条成功");
		setAttr("newWord", word);
		render("/WEB-INF/pages/fc/newword/show.jsp");
	}
	/**
	 * 置顶
	 */
	public void top(){
		NewWord word = NewWord.dao.findById(getParaToInt(0)).set("isTop", 1);
		word.update();
		LogUtil.log.info("置顶word[id:"+getParaToInt(0)+"]词条成功");
		setAttr("newWord", word);
		render("/WEB-INF/pages/fc/newword/show.jsp");
	}
	/**
	 * 取消置顶
	 */
	public void notop(){
		NewWord word = NewWord.dao.findById(getParaToInt(0)).set("isTop", 0);
		word.update();
		LogUtil.log.info("取消置顶word[id:"+getParaToInt(0)+"]词条成功");
		setAttr("newWord", word);
		render("/WEB-INF/pages/fc/newword/show.jsp");
	}
	/**
	 * 分享
	 */
	public void share(){
		NewWord word = NewWord.dao.findById(getParaToInt(0)).set("isShare", 1);
		word.update();
		LogUtil.log.info("分享word[id:"+getParaToInt(0)+"]词条成功");
		setAttr("newWord", word);
		render("/WEB-INF/pages/fc/newword/show.jsp");
	}
	/**
	 * 取消分享
	 */
	public void noshare(){
		NewWord word = NewWord.dao.findById(getParaToInt(0)).set("isShare", 0);
		word.update();
		LogUtil.log.info("取消分享word[id:"+getParaToInt(0)+"]词条成功");
		setAttr("newWord", word);
		render("/WEB-INF/pages/fc/newword/show.jsp");
	}
	
}
